package com.aeks.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.aeks.user.AboutUser;

public class LoginServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		
		String msg = "error";
		String action = request.getParameter("action");
		if (action != null && action.equals("login")) {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			
			boolean isSuccess = AboutUser.check(username, password);
			if (isSuccess) {
				HttpSession session = request.getSession();
				session.setAttribute("username", username);
				msg = "ok";
				response.sendRedirect("main.jsp");
			} 
		}
		
		//msg = new String(msg.getBytes("ISO-8859-1"), "utf-8");
		//out.write(msg);
		out.flush();
		out.close();
	}

}
